package com.attendance.www.hr.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.dao.mapper.AttachmentMapper;
import com.attendance.www.hr.entitys.Attachment;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IHolidayExcelService;
import com.attendance.www.hr.service.IHolidayImportService;



@Service
public class HolidayImportServiceImpl implements IHolidayImportService {

	@Resource
	private AttachmentMapper attachmentDao;

	@Resource
	private IHolidayExcelService holidayExcelService;

	
	/**
	 * 导入基础数据库
	 */
	@Override
	public List<String> importCode(Integer attachmentId,String type,Integer userId) throws Exception {
		List<String> successList =new ArrayList<String>();
		// 查询附件表获取路径地址
		Attachment attachment = attachmentDao.selectByPrimaryKey(attachmentId);
		// 设置文件路径
		System.out.println(11111111);
		System.out.println(attachment.getUrl());
		String pathName = attachment.getUrl().replace(AppConstant.UPLOAD_RESOURCE, AppConstant.SAVE_FILE_REPOSITORY);
		File file = new File(pathName);
		Workbook work = null;
		if (file.exists()) {
			try {
				InputStream inputStream = new FileInputStream(file);
				if (pathName.matches("^.+\\.(?i)(xls)$")) {
					work = new HSSFWorkbook(inputStream);
				} else if (pathName.matches("^.+\\.(?i)(xlsx)$")) {
					work = new XSSFWorkbook(inputStream);
				}
			} catch (Exception ex) {
				throw new ResultFaultException("0_00003");
			}
		} else {
			throw new ResultFaultException("0_00003");
		}
		if (work == null) {
			throw new ResultFaultException("0_00003");
		}
		
		if(work != null){
			//客户导入
			List<String> holidayList=holidayExcelService.importHoliday(work, type);
			if(holidayList.size()>0){
				successList.addAll(holidayList);
			}
			//项目导入
			
		}
		return successList;
	}
	
//	/**
//	 * 清除组织架构信息
//	 * @return
//	 */
//	@SuppressWarnings("unused")
//	private boolean delOrganizationalStructure(){
//		//删除成本项导入
//		costitemExcelService.delCostitem();
//		return true;
//	}
}
